var AlarmCountInfo = function (alarm) {
  BaseModel.call(this);

  var alarmPageSize = 5;

  var self = this;
  var fn = {
    initData: function (alarm) {
      var machineAlarmData = this.getMachineAlarmData(alarm);
      var machineTextList = _.map(machineAlarmData, 'id');
      self.machineAlarmDataIds = machineTextList.join(',');
      self.machineAlarmData = machineAlarmData;

      var summaryAlarmData = this.getSummaryAlarmData(alarm);
      var summaryTextList = _.map(summaryAlarmData, 'id');
      self.summaryAlarmData = summaryTextList.join(',');
      self.summaryAlarmData = summaryAlarmData;
      self.summaryAlarmIndex = 0;
    },

    getMachineAlarmData: function (alarm) {
      var machineAlarmData = {};
      var alarmYearList = alarm.cus_year || [];
      var maxAlarmCount = 0;
      for (var i = 0; i < alarmYearList.length; i++) {
        var alarmYear = alarmYearList[i];
        var name = alarmYear.name;
        var count = alarmYear.count;
        if (maxAlarmCount < count) {
          maxAlarmCount = count;
        }

        machineAlarmData[name] = {
          name: name,
          yearCount: count,
          monthCount: 0
        };
      }

      var alarmMonthList = alarm.cus_month || [];
      for (var i = 0; i < alarmMonthList.length; i++) {
        var alarmMonth = alarmMonthList[i];
        var name = alarmMonth.name;
        var alarmData = machineAlarmData[name];
        if (alarmData === undefined) {
          alarmData = {
            name: name,
            yearCount: 0,
            monthCount: alarmMonth.count
          };
          machineAlarmData[name] = alarmData;
        } else {
          alarmData.monthCount = alarmMonth.count;
        }
      }

      machineAlarmData = _.values(machineAlarmData);
      machineAlarmData = _.orderBy(machineAlarmData, ['yearCount'], ['desc']);
      for (var i = 0; i < machineAlarmData.length; i++) {
        var obj = machineAlarmData[i];
        obj.id = [obj.name, obj.yearCount, obj.monthCount].join('_')
      }

      maxAlarmCount = Math.ceil(_.round(maxAlarmCount / 5, 2)) * 5;
      self.maxAlarmCount = maxAlarmCount;
      return machineAlarmData;
    },

    getSummaryAlarmData: function (alarm) {
      var alarmList = alarm.part || [];
      var sumCount = _.sumBy(alarmList, 'count');

      var maxSummaryCount = 0;
      var alarmDataList = [];
      for (var i = 0; i < alarmList.length; i++) {
        var alarm = alarmList[i];
        var count = alarm.count;
        var rate = count / sumCount * 100;
        rate = _.round(rate, 2);
        if (rate < 1) {
          continue;
        }

        if (maxSummaryCount < count) {
          maxSummaryCount = count;
        }
        var name = alarm.name;
        alarmDataList.push({
          id: `${name}_${count}`,
          name: name,
          count: count,
          rate: rate
        });
      }

      alarmDataList = _.orderBy(alarmDataList, ['count'], ['desc']);
      self.maxSummaryCount = Math.ceil(_.round(maxSummaryCount / 5, 2)) * 5;
      return alarmDataList;
    }
  };

  this.incrementDayIndex = function () {
    var pageSize = alarmPageSize;
    var totalCount = this.summaryAlarmData.length;
    if (totalCount <= pageSize) {
      return false;
    }

    var totalPage = Math.ceil(_.round(totalCount / pageSize, 2));
    var currentPage = this.summaryAlarmIndex / pageSize;
    currentPage = (currentPage + 1) % totalPage;
    this.summaryAlarmIndex = currentPage * pageSize;
    return true;
  };

  this.getMachineChartData = function (pagination) {
    var currentPage = pagination.currentPage;
    var pageSize = pagination.pageSize;
    var startIndex = (currentPage - 1) * pageSize;

    var machineAlarmData = this.machineAlarmData;
    machineAlarmData = machineAlarmData.slice(startIndex, startIndex + pageSize);

    var len = machineAlarmData.length;
    for (var i = len; i < pageSize; i++) {
      machineAlarmData.push({
        name: i
      });
    }

    return machineAlarmData;
  };

  this.getAlarmChartData = function () {
    var pageSize = alarmPageSize;
    var summaryAlarmData = this.summaryAlarmData;
    var summaryAlarmIndex = this.summaryAlarmIndex;
    summaryAlarmData = summaryAlarmData.slice(summaryAlarmIndex, summaryAlarmIndex + pageSize);

    var len = summaryAlarmData.length;
    for (var i = len; i < pageSize; i++) {
      summaryAlarmData.push({
        name: i
      });
    }

    return summaryAlarmData;
  };

  this.resetAlarmData = function (alarm) {
    var machineAlarmData = fn.getMachineAlarmData(alarm);
    var machineTextList = _.map(machineAlarmData, 'id');
    this.machineAlarmDataIds = machineTextList.join(',');
    this.machineAlarmData = machineAlarmData;

    var summaryAlarmData = fn.getSummaryAlarmData(alarm);
    var summaryTextList = _.map(summaryAlarmData, 'id');
    this.summaryAlarmData = summaryTextList.join(',');
    this.summaryAlarmData = summaryAlarmData;

    var totalCount = this.summaryAlarmData.length;
    if (this.summaryAlarmIndex >= totalCount) {
      this.summaryAlarmIndex = 0;
    }
  };

  fn.initData(alarm);
};
